package user

import (
	"context"
	"fmt"
	"gitee.com/zyw0605688_admin/gozero/user/model"
	"github.com/duke-git/lancet/v2/cryptor"

	"gitee.com/zyw0605688_admin/gozero/user/internal/svc"
	"gitee.com/zyw0605688_admin/gozero/user/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserRegisterLogic) UserRegister(req *types.UserRegisterRequest) (resp *types.UserRegisterResponse, err error) {
	md5Password := cryptor.Md5String(req.Password)

	item := model.User{
		UserName: req.UserName,
		Password: md5Password,
		Email:    req.Email,
	}
	fmt.Println(item)
	err = l.svcCtx.DB.Save(&item).Error
	if err != nil {
		logx.Error("保存数据库出错", err)
		return nil, err
	}
	return
}
